package com.example.mapv2;


import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.MapFragment;

import android.location.Location;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {

	private GoogleMap googleMap;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initilizeMap();
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	
	
	 @Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initilizeMap();
	}

	private void initilizeMap() {
	        if (googleMap == null) {
	            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
	                    R.id.map)).getMap();
	            
	            googleMap.setMyLocationEnabled(true);
	    		googleMap.setOnMyLocationChangeListener(new OnMyLocationChangeListener() {
	    			
	    			@Override
	    			public void onMyLocationChange(Location arg0) {
	    				// TODO Auto-generated method stub
	    				double latitude = arg0.getLatitude();
	    				double longitude = arg0.getLongitude();
	    				LatLng latLg= new LatLng(latitude, longitude);
	    				googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLg));
	    				googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
	    			}
	    		});
	 
	            // check if map is created successfully or not
	            if (googleMap == null) {
	                Toast.makeText(getApplicationContext(),
	                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
	                        .show();
	            }
	        }
	        
	    }
	
	
	 
	

}
